<template>
  <div class="container">
    <div class="center">
      <div class="loginimg">
        <img :src="Img" alt />
      </div>
      <div class="from">
        <h1>欢迎来到飞翔</h1>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
          <el-form-item prop="username">
            <i class="i-first">
              <img :src="Shouji" alt />
            </i>
            <el-input class="first" v-model="ruleForm.username" placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item prop="pwd">
            <i class="i-two">
              <img :src="Suo" alt />
            </i>
            <el-input class="two" type="password" v-model="ruleForm.pwd" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item prop="pwd">
            <i class="i-two">
              <img :src="Suo" alt />
            </i>
            <el-input class="two" type="password" v-model="ruleForm.pwd2" placeholder="请输入确认密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
          </el-form-item>
          <p @click="loginfun">已有账号，返回登录</p>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import Img from "../assets/img/chatu.png";
import Shouji from "../assets/img/shouji.png";
import Suo from "../assets/img/suoding.png";
import { getLogin } from "../api/loginApi";
import { setToken } from "../mock/cookieUtils";
export default {
  data() {
    return {
      Img: Img,
      Shouji: Shouji,
      Suo: Suo,
      ruleForm: {
        username: "",
        pwd: "",
        pwd2: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 6,
            max: 13,
            message: "长度在 6 到 13 个字符",
            trigger: "blur",
          },
        ],
        pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
        pwd2: [{ required: true, message: "请输入确认密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    loginfun() {
      window.location.href = "http://localhost:8080/#/login";
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
          getLogin(this.ruleForm).then((res) => {
            console.log("返回值", res);
            if (res.code == 200) {
              setToken("token", res.data.token);
              this.$router.push("/");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang = 'less' scoped>
.container {
  width: 100%;
  height: 100%;
  background-image: url("../assets/img/background.webp");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  position: relative;
  .center {
    width: 720px;
    height: 500px;
    background-color: white;
    // opacity: 0.5;
    position: absolute;
    border-radius: 20px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .loginimg {
      width: 258px;
      height: 312px;
      background-color: skyblue;
      margin-top: 88px;
      margin-left: 67px;
      display: inline-block;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .from {
      width: 274px;
      height: 374px;
      float: right;
      margin-top: 88px;
      margin-right: 43px;
      position: relative;
      h1 {
        color: #3d8ffd;
        text-align: center;
        line-height: 1;
      }
      .el-input,
      .el-button {
        width: 274px;
        height: 40px;
        border-radius: 5px;
        /* margin-top: 66px; */
        /* float: left; */
      }
      .i-first > img {
        position: absolute;
        left: 3px;
        top: 60px;
        width: 12px;
        height: 16px;
        transform: translateY(20px);
        z-index: 10;
      }
      .i-two > img {
        position: absolute;
        left: 3px;
        top: -10px;
        width: 12px;
        height: 16px;
        transform: translateY(20px);
        z-index: 10;
      }
      /deep/ .first {
        margin-top: 66px;
        height: 40px;
        line-height: 40px;
      }
      /deep/.el-input__inner {
        height: 40px;
      }
      span {
        color: #3d8ffd;
      }
      span:nth-of-type(1) {
        float: left;
      }
      span:nth-of-type(2) {
        float: right;
      }
      .el-button {
        margin-top: 20px;
      }
      p {
        float: right;
        color: #3d8ffd;
      }
    }
  }
}
</style>